package cn.jinbyte.core.handler.crypt;

import cn.jinbyte.core.utils.Sm4Utils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Sm4CryptHandler implements CryptHandler {
    private final String key;

    public Sm4CryptHandler(String key) {
        this.key = key;
    }

    @Override
    public <T> String encode(T content) {
        if (content == null) {
            return null;
        }
        return Sm4Utils.encrypt(serializeToString(content, content.getClass()), key);
    }

    @Override
    public <T> T decode(String encryptedValue, Class<T> tClass) {
        String decrypt = Sm4Utils.decrypt(encryptedValue, key);
        return parseFromString(decrypt, tClass);
    }
}
